package com.xsimas.xyybackend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsimas.xyybackend.domain.Product;
import com.xsimas.xyybackend.dto.PageRequestDTO;
import com.xsimas.xyybackend.pojo.Result;
import com.xsimas.xyybackend.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RAY_HUI
 * @date 2025-07-09
 * @time 19:00
 */

@RestController
@RequestMapping("/api/product")
@Tag(name = "产品管理接口", description = "提供产品的增删查改及分页操作")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页查询产品信息
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询产品数据", description = "根据页码和每页数量分页查询产品信息")
    public Result<IPage<Product>> getProductsByPage(@RequestBody PageRequestDTO pageRequest) {
        Page<Product> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        return Result.ok(productService.page(page));
    }

    /**
     * 查询所有产品
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有产品列表", description = "返回所有产品记录")
    public Result<List<Product>> getAllProducts() {
        return Result.ok(productService.list());
    }

    /**
     * 根据ID查询产品
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取单个产品", description = "通过ID查询产品详情")
    public Result<Product> getProductById(
            @Parameter(description = "产品ID", required = true)
            @PathVariable Long id) {
        return Result.ok(productService.getById(id));
    }

    /**
     * 新增产品
     */
    @PostMapping("/add")
    @Operation(summary = "新增一个产品记录", description = "将传入的产品对象保存到数据库")
    public Result<Boolean> createProduct(
            @Parameter(description = "产品实体对象", required = true)
            @RequestBody Product product) {
        boolean success = productService.save(product);
        return success ? Result.ok(success) : Result.error(400, "新增产品失败");
    }

    /**
     * 更新产品
     */
    @PutMapping("/update")
    @Operation(summary = "更新一个产品记录", description = "根据ID更新产品信息")
    public Result<Boolean> updateProduct(
            @Parameter(description = "产品实体对象", required = true)
            @RequestBody Product product) {
        boolean success = productService.updateById(product);
        return success ? Result.ok(success) : Result.error(400, "更新产品失败");
    }

    /**
     * 删除产品
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除一个产品", description = "逻辑或物理删除指定ID的产品")
    public Result<Boolean> deleteProduct(
            @Parameter(description = "产品ID", required = true)
            @PathVariable Long id) {
        boolean success = productService.removeById(id);
        return success ? Result.ok(success) : Result.error(400, "删除产品失败");
    }
}